package nl.boelen.game.client;

import java.applet.Applet;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import nl.boelen.game.handler.WindowHandler;
import nl.boelen.game.net.packet.Packet11Leave;
import nl.boelen.game.options.Option;

@SuppressWarnings("serial")
public class Launcher extends Applet {
    private static Client client = new Client();

    @Override
    public void init() {
        setLayout(new BorderLayout());
        add(client, BorderLayout.CENTER);
        setMaximumSize(Client.DIMENSION);
        setMinimumSize(Client.DIMENSION);
        setPreferredSize(Client.DIMENSION);
    }

    @Override
    public void start() {
        client.start();
    }

    @Override
    public void stop() {
        client.stop();
    }

    public static void main(String[] args) {
        client.setPreferredSize(Client.DIMENSION);
        client.setMinimumSize(Client.DIMENSION);
        client.setMaximumSize(Client.DIMENSION);

        client.frame = new JFrame(Option.GameName);
        client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        client.frame.setLayout(new BorderLayout());
        client.frame.setResizable(false);
        client.frame.setVisible(true);
        client.frame.add(client, BorderLayout.CENTER);
        client.frame.pack();
        client.frame.setLocationRelativeTo(null);

        client.windowHandler = new WindowHandler() {
            @Override
            public void windowClosed() {
                Packet11Leave packet = new Packet11Leave(client.player.getUserName(), client.player.getUniqueKey());
                client.TCPSender.sendPacket(packet);
            }
        };
        client.frame.addWindowListener(client.windowHandler);

        client.start();
    }
}
